<template>
    <el-container class="layout-container">
        <!-- 左侧导航栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
            <el-menu :default-active="activeMenu" class="el-menu-vertical" :collapse="isCollapse"
                background-color="#304156" text-color="#bfcbd9" active-text-color="#409EFF" router>
                <template v-for="(item, index) in menuList" :key="index">
                    <el-menu-item :index="item.path" @click="handleMenuClick(item)">
                        <el-icon>
                            <component :is="getIconComponent(item.icon)" />
                        </el-icon>
                        <template v-if="!isCollapse">
                            <span>{{ item.menuName }}</span>
                        </template>
                    </el-menu-item>
                </template>
            </el-menu>
        </el-aside>

        <!-- 主页面区域 -->
        <el-container>
            <el-header class="header">
                <div class="header-left">
                    <el-button text circle @click="toggleSidebar">
                        <el-icon size="20">
                            <Fold v-if="!isCollapse" />
                            <Expand v-else />
                        </el-icon>
                    </el-button>
                    <h2 class="system-title">滴滴后台管理系统</h2>
                </div>

                <el-dropdown>
                    <span class="el-dropdown-link">
                        管理员
                        <el-icon>
                            <ArrowDown />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click="open">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>

            <el-main class="main">
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { Fold, Expand, ArrowDown, Search, Refresh, Plus, Delete, Edit, View } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, messageConfig } from 'element-plus'
import request from '@/utils/request'

const router = useRouter()
const isCollapse = ref(false)
const menuList = ref([])
const activeMenu = ref('')
const showAddDialog = ref(false)
const roleOptions = ref([])
const formRef = ref(null)

// 页面加载时获取菜单
onMounted(() => {
    getMenuList()
})

// 动态获取图标组件
const getIconComponent = (iconName) => {
    return ElementPlusIconsVue[iconName] || ElementPlusIconsVue['Menu']
}

// 获取菜单数据
const getMenuList = async () => {
    try {
        const userStr = sessionStorage.getItem('user')
        // if (!userStr) {
        //     ElMessage.error('用户未登录')
        //     router.push('/login')
        //     return
        // }

        const user = JSON.parse(userStr)
        const username = user.username


        request.get(`/menu/list?username=${encodeURIComponent(username)}`)
            .then(res => {
                // console.log(res);
                menuList.value = res.data.menu
                activeMenu.value = menuList.value[0].path
            }).catch(err => {
                console.log(err);

            })
    } catch (error) {
        ElMessage.error(error.message || '获取菜单失败')
        console.error('获取菜单失败:', error)
    }
}

// 点击菜单跳转
const handleMenuClick = (item) => {
    if (item.path) {
        router.push(item.path)
        activeMenu.value = item.path
    }
}

// 折叠菜单
const toggleSidebar = () => {
    isCollapse.value = !isCollapse.value
}
const open = () => {
    ElMessageBox.confirm(
        '是否退出登录？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            ElMessage({
                type: 'success',
                message: '退出成功！',
            });
            router.push('/login');
            sessionStorage.clear();
        })
        .catch(() => {
            // ElMessage({
            //     type: 'success',
            //     message: 'Delete canceled',
            // })
        })
}
</script>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.layout-container {
    height: 100vh;
}

.aside {
    background-color: #304156;
    transition: width 0.3s ease;
    overflow: hidden;
}

.el-menu-vertical {
    border-right: none;
    height: 100%;
}

.header {
    background-color: #fff;
    border-bottom: 1px solid #e6e6e6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    height: 60px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.system-title {
    font-size: 18px;
    color: #333;
    margin-left: 10px;
}

.header-right {
    display: flex;
    align-items: center;
}

.el-dropdown-link {
    cursor: pointer;
    display: flex;
    align-items: center;
    color: #409EFF;
}

.main {
    background-color: #f5f7fa;
    padding: 20px;
    min-height: calc(100vh - 60px);
}


</style>
